package com.tang.multi.v3;

import lombok.SneakyThrows;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Tang
 * @classname WorkHandler
 * @description [ 数据监听，业务处理 ]
 * @date 2021/8/10 14:37
 */
public class WorkHandler implements Runnable {

    private SocketChannel socketChannel;

    private SelectionKey selectionKey;

    private Selector selector;

    private ExecutorService executorService = Executors.newFixedThreadPool(2);

    public WorkHandler(SocketChannel socketChannel, Selector selector, SelectionKey selectionKey) {
        this.socketChannel = socketChannel;
        this.selectionKey = selectionKey;
        this.selector = selector;
    }

    @SneakyThrows
    @Override
    public void run() {
        // 线程池处理数据
        executorService.execute(new Process(this.selectionKey, this.socketChannel));
    }
}